<?php

	fta_registerModules(
		array(
		"FTAEvents/FTAEvents"
		)
	);

	class MAIEvents extends FTAEvents {
		
		
		function MAIEvents(){
			parent::__construct();
			
			/* SHOULD ALL BE DONE IN PARENT
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTADateFormat;
			$this->dateFormat = (0 < strlen($FTADateFormat)) ? $FTADateFormat : "d F y";*/
						
		}
		
		function getWhatsOn($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			//this gets exhibitions
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docExhibit');
						
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("startdate");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->addConstraint("enddate", date('Y-m-d H:i:s'), ">=");
			
			$this->cmsDocument->resetIterator();
			
			$allExhibitions = $this->cmsDocument->getAllDocuments();
			
			if (!$allExhibitions)
				$allExhibitions = array();
				
			$exhibitionList = array();
			
			foreach ($allExhibitions as $exhibition){
				//$exhibitionList[$exhibition->startdate] = $exhibition;
				if(isset($exhibitionList[$exhibition->startdate])){
					//problem create new key
					$count = 1;
					$key = date("Y-m-d H:i:s", strtotime($exhibition->startdate)+$count);
					while (isset($exhibitionList[$key])) {
						$count++;
						$key = date("Y-m-d H:i:s", strtotime($exhibition->startdate)+$count);
					}
					$exhibitionList[$key] = $exhibition;
					
				} else {
					$exhibitionList[$exhibition->startdate] = $exhibition;
				}
			}
			//echo "exhibitions = <pre>";print_r($exhibitionList);echo "</pre>";
			
			//get events
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docEvent');
						
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("documentyear");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->addConstraint("documentyear", date("Y-m-d"), ">");
			
			$this->cmsDocument->resetIterator();
			
			$allEvents = $this->cmsDocument->getAllDocuments();
			
			if (!$allEvents)
				$allEvents = array();
				
			$eventList = array();
			
			foreach ($allEvents as $event){
				//$eventList[$event->documentyear] = $event;
				if(isset($eventList[$event->documentyear])){
					//problem create new key					
					$count = 1;
					$key = date("Y-m-d H:i:s", strtotime($event->documentyear)+$count);
					while (isset($eventList[$key])) {
						$count++;
						$key = date("Y-m-d H:i:s", strtotime($event->documentyear)+$count);
					}
					$eventList[$key] = $event;
				} else {
					$eventList[$event->documentyear] = $event;
				}
			}
			
			//merge the two arrays
			//$allDocuments = array_merge($exhibitionList, $eventList);
			//loop through one adding it then loop through the other adding and checking for same date
			$allDocuments = $exhibitionList;
			foreach ($eventList as $event){
				if(isset($allDocuments[$event->documentyear])){
					//problem create new key
					$count = 1;
					$key = date("Y-m-d H:i:s", strtotime($event->documentyear)+$count);
					while (isset($allDocuments[$key])) {
						$count++;
						$key = date("Y-m-d H:i:s", strtotime($event->documentyear)+$count);
					}
					$allDocuments[$key] = $event;
				} else {
					$allDocuments[$event->documentyear] = $event;
				}
			}
			
			//sort array
			ksort($allDocuments);
			
			//echo "all documents = <pre>";print_r($allDocuments);echo "</pre>";
			
			return $allDocuments;
						
		}
		
		function renderNewsList($pageSize = null, $pageNumber = null, $sort = null, $direction = null){ 
			
			$news = $this->getNews($pageSize, $pageNumber, $sort, $direction);
		
			if (0 == count($news))	return;
			
			if(is_null($this->renderNewsListFunction)) {
				
				global $FTANewsListItemRenderFunction;
				if(!isset($FTANewsListItemRenderFunction)){
					$FTANewsListItemRenderFunction = '$o=fta_getSharedNewsObject(); echo $o->renderNewsListItemFunction($item);';
				}
	
				$this->renderNewsListFunction  = create_function('$item', $FTANewsListItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			fta_widgetList($news,"exhibition-list","altItem","firstItem", "lastItem",$this->renderNewsListFunction);
		}
		
		function getEventDatesForCalendar(){
			
			$news = $this->getNews();
			
			$eventDates = array();
			
			foreach($news as $item){
				
				if(isset($eventDates[date("m/d/Y", strtotime($item->documentyear))])){
					$eventDates[date("m/d/Y", strtotime($item->documentyear))] .= " <a href='/Events/' class='tip_link_event' >" . $item->title . "</a> ";
				} else {
					$eventDates[date("m/d/Y", strtotime($item->documentyear))] = " <a href='/Events/' class='tip_link_event' >" . $item->title . "</a> ";
				}
			}
			
			return $eventDates;
		}
		
		function renderWhatsOnList($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			//1. Get combined list of items
			$whatsOnList = $this->getWhatsOn($pageSize, $pageNumber, $sort, $direction);
			if (0 == count($whatsOnList))	return;
			
			//2. render them
			foreach ($whatsOnList as $item){
				if($item->documenttypecode == 'docExhibit'){
					$date = $item->startdate;
					$newsCategory = "Exhibition";
					
					if($item->author == null || $item->author == "")
					{
						$description = $item->authordepartment;
					}
					else
					{
						$description = $item->author;
					}
					if($item->startdate > date("Y-m-d H:i:s")){
						$url = "/Exhibitions/Coming-Exhibitions/";
					} else {
						$url = "/Exhibitions/Currently-Showing/";
					}
				} else {
					$date = $item->documentyear;
					$newsCategory = "Event";
					if($item->content == null || $item->content == "")
					{
					$description = $item->shortdescription;
					}
					else
					{
						$description = $item->content;
					}
					$url = "/Events/";
				}
				
				?>
					<li>
                    	<div class="date"><strong><?php echo date("M", strtotime($date)); ?></strong> <br /><?php echo date("Y", strtotime($date)); ?></div>
                        <div class="news-item">
                            <h4><a href="<?php echo $url; ?>"><?php echo $item->title; ?></a></h4>
                            <div class="news-category"><?php echo $newsCategory; ?></div>
                            <?php echo $description; ?>
                        </div>
					</li>  
				
				
				<?php
			}
			
		}
	}
	
	function mai_getWhatsOn($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
		ob_start();
		$newsObject = mai_getSharedEventsObject();
		$newsObject->renderWhatsOnList($pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();	
	}
	
	function mai_getEventDatesForCalendar(){
		$newsObject = mai_getSharedEventsObject();
		return $newsObject->getEventDatesForCalendar();
	}
	
	function mai_getEventsList($pageSize = null, $pageNumber = null, $sort = null, $direction = null){		
		ob_start();
		$newsObject = mai_getSharedEventsObject();
		$newsObject->renderNewsList($pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();				
	}
	
	function mai_getSharedEventsObject(){

		global $fta_;

		global $MAIEventsClass;
		
		if(!is_null($MAIEventsClass) && "MAIEvents" != $MAIEventsClass){
		} else {
			$MAIEventsClass = "MAIEvents";
		}
			
		if (!array_key_exists('maiEvents',$fta_)){
			$fta_->maiEvents = new  $MAIEventsClass();
		}
		return $fta_->maiEvents;
	}
